#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import struct

base_path = r'/rootfs/media/kasim/DataSet/FaceVideo/CropImageMargin'
out_path = r'/rootfs/media/kasim/DataSet/FaceVideo/CropImageMargin'
bbox_count_list_path = r'/rootfs/media/kasim/DataSet/FaceVideo/CropImageMargin/bbox_count_list.txt'
bbox_list_path = r'/rootfs/media/kasim/DataSet/FaceVideo/CropImageMargin/bbox_list.txt'

score_thr = 0.5


def main():
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    os.system('chmod a+wr {}'.format(out_path))

    out_list_file_path = os.path.join(out_path, 'file_bbox_list_{}.txt'.format(score_thr))

    with open(out_list_file_path, 'w') as out_list_file:
        with open(bbox_count_list_path, 'r') as bbox_count_file:
            with open(bbox_list_path, 'r') as bbox_file:
                for line in bbox_count_file.readlines():
                    lines = line.strip().split(',')
                    file_dir = os.path.splitext(lines[0])[0]

                    bbox_count = int(lines[1])
                    for _ in range(bbox_count):
                        bbox_infos = bbox_file.readline()
                        bbox_infos = bbox_infos.strip().split()
                        file_name = bbox_infos[0]
                        bbox = bbox_infos[1].split(',')
                        score = float(bbox[-1])
                        if score < score_thr:
                            continue
                        file_path = os.path.join(file_dir, file_name)
                        bbox_info1 = bbox_infos[1]
                        bbox_info2 = bbox_infos[2]
                        out_info_str = '{} {} {}\n'.format(file_path, bbox_info1, bbox_info2)
                        out_list_file.write(out_info_str)

    os.system('chmod a+wr {}'.format(out_list_file_path))

    print('finish!!!!')


if __name__ == '__main__':
    main()
